<?php
/**
 * Created by PhpStorm.
 * User: wolf
 * Date: 2018/6/25
 * Time: 17:56
 */

namespace App\Tools;
class Map{
    /*
     * 根据地址赖获取经纬度
     * @param $address
     */
    public static function getLngLat($address){
        //http://api.map.baidu.com/geocoder/v2/?callback=renderOption&output=json&address=百度大厦&city=北京市&ak=您的ak
        if(!$address){
            return '';
        }
        $data=[
            'address'=>$address,
            'key'=>config('map.key'),
            'output'=>'json',
        ];
        $url=config('map.tengxu_map_url').config('map.address').'?'.http_build_query($data);
        // 1 file_get_contents($data)
        // 2 curl
        $result=doCurl($url,0,$data);
        //curl 失败时使用get
        if($result===false){
            $result = file_get_contents($url);
        }

        $res = json_decode($result,true);
        if($res['status']==0){
            return [
                'lat' => $res['result']['location']['lat'],
                'lng' =>  $res['result']['location']['lng']
            ];
        }else{
            return '';
        }
        
    }

}